// let enumIdList = new Array();

/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let index;


    form.verify({
        minSort: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (value < 1000){
                return "排序号不能小于1000";
            }
        }, isPositiveNumber: function(value,item){
            if (!/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
        },tariffName:function (value) {
            if(!/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value)){
                return "计价器名称只能输入字母、数字、汉字";
            }
        }
    });

    zmnLayUtil.layFormVerify(form);

    // 分类下拉框联动
    zmnProduct.laySelectSpecial(form, "/showProduct/listChilds.action", "/showProduct/listCateChilds.action", "", "servCategId", "categOneId", "categId", "");

    form.render(null, "zmn-form");

    // 提交
    form.on('submit(zmn-form-submit)', function (obj) {
        let data = obj.field;
        let bizTypeArr = new Array();

        let bizType = document.getElementsByName("bizType");
        for(let k in bizType){
            if(bizType[k].checked)
                bizTypeArr.push(bizType[k].value);
        }

        if(bizTypeArr.length == 0){
            layer.msg("业务类型不能为空");
            return false;
        }
        data.bizType = bizTypeArr.join("$");

        let url = "/tariffProduct/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            beforeSend: function () {
                index = layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            },
            success: function (json) {
                if(json.status == 200) {
                    layer.msg("操作成功",{time: 1000},()=>{
                        zmnLayUtil.closeThisIframe(parent);
                    });
                } else {
                    layer.msg(json.message, {"icon": 2});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            complete:function () {
                layer.close(index);
            }
        })

        return false;
    });

    //对外暴露的接口
    exports('product_tariff_edit_js', {});
});

var zmnProduct = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url1 selectOneId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param url1 selectTwoId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     * @param selectThrId 第3级select的ID，不含#号，如无第3级则两级联动
     */
    laySelectSpecial: function (form, url1, url2, url3, selectOneId, selectTwoId, selectThrId, selectFourId) {
        let $ = layui.$;
        let servCategId;
        let categOneId;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return layer.alert("请选择！");
            }
            servCategId = obj.value;
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {parentId: servCategId},
                success: function (items) {
                    var optionHtml = '<option value="">请选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    $("#" + selectThrId).html('<option value="">请选择</option>');
                    $("#" + selectFourId).html('<option value="">请选择</option>');
                    form.render("select");
                }
            });
        });
        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        }
        ;

        form.on("select(" + selectTwoId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return layer.alert("请选择！");
            }
            categOneId = obj.value;
            servCategId = $('#servCategId').val();;
            $.ajax({
                type: "GET", url: url2,
                dataType: "json", data: {categOneId: obj.value, servCategId: servCategId},
                success: function (items) {
                    let optionHtml = '<option value="">请选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectThrId).html(optionHtml);
                    $("#" + selectFourId).html('<option value="">请选择</option>');
                    form.render("select");
                }
            });
        });

        if (zmnUtil.isEmptyNull(selectFourId)) {
            return;
        }
        ;

    },
}
